<?php
//session_start();
require_once("admin_functions.php");
chk_session();
// Start the session

/**************************************************************
File Name		: importData.php
Purpose			: To Import database from earlier exported versions.
Author			: Vishal Saraf
Create Date		: 07/18/2002
Last Modified	: 07/22/2002
***************************************************************/

include ("../dbconnect.php");
// All Messages
//include("../message.php");

// Files included from dbutility
include_once("global_conf.php");
include_once("lib/main.php");
include_once("lib/exportData.php");
include_once("lib/functions.php");
$phpMyBackup=new phpMyBackup;
$databaseBackUp = new databaseBackUp();
// Ends here
?>

<Script Language="JavaScript">
<!--

function showprompt(targeturl)
{
	if(confirm("Are you sure you want to delete the data"))
	{
		self.location.href=targeturl;
	}
	return;
}

function popUp(str)
{
	window.open(str,"tinyWindow",'toolbar=no,left=200,width=300,height=300');
}
function getFileName()
{
	var file=prompt('Please enter File Name:',' ');
	if(file==null)
	{
		return false;
	}
	else
	{
		document.frm.fileName.value=file;
		return true;
	}
}
//-->
</Script>

<html>
<head>
<title>DB Utility</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../../include/style.css" type="text/css">
<link rel="stylesheet" href="../style.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
	<tr><td align=left>
		<?php include "menu.php"; ?></TD>
			<tc>
				<td valign="top" class='bodytextb'>

<table  width='100%' border="0" cellspacing="2" cellpadding="0" >
	<tr>
		<td colspan=4 class='subheadinglink' align='center' valign='middle'>
			DB Utility
		</td>
	</tr>
	<tr>
		<td width='25%' class='subheadinglink' align='center' valign='middle'>
			<a href='exportData.php'><font class='subheadinglink'>Export</font></a>
		</td>
		<td width='25%' class='subheadinglink' align='center' valign='middle'>
			<a href='importData.php'><font class='subheadinglink'>Import</font></a>
		</td>
		<td width='25%' class='subheadinglink' align='center' valign='middle'>
			&nbsp;
		</td>
		<td width='25%' class='subheadinglink' align='center' valign='middle'>
			&nbsp;
		</td>
	</tr>
</table>
<?php
if(!empty($mesg))
{
	print "<font class='mesg'>$mesg</font>";
}
else
{
	print "<font class='mesg'>&nbsp;&nbsp;</font>";
}
?>
<?php

$handle=opendir('./export');
$all_files=array();

// remove old backups
delOldBackups();

// find the time to remove local backups
while (false !== ($file = readdir($handle))) {
	if ($file != "." && $file != "..") {
		//check if it is a sql file, and it is in the correct format
		$file_info_arr=explode(".",$file);
		$filtype=$file_info_arr[1];
		if(strtolower($filtype)=="sql") {
			$all_files[]=$file;
		}
	}
}

$printed_titles=array();
natsort($all_files);
$arrayLength = sizeOf($all_files);
if($arrayLength > 0)
{
	echo"<br><table cellspacing='1' border='0' cellpadding='1' width='550' align='left'>";
	echo"<tr><th align='left' class='thdark'>File Name</th><th align='left' class='thdark'>Date</th><th align='left' class='thdark'>File Size</th><th align='left' class='thdark'>Import</th><th align='left' class='thdark'>Detail</th><th align='left' class='thdark'>Action</th></tr>";
}
$i=1;
foreach($all_files as $file) {
	$file_info1=explode("_", $file);
	$db   = $file_info1[0];
	$dbName = $file_info1[1];
	$db = $db."_".$dbName;
	$time = $file_info1[2];
	if(!isset($printed_titles[$db])) {
		$printed_titles[$db]=1;
	}
	$filesize_b=filesize("./export/".$file);
	$filesize=round($filesize_b/1024,0);
	$month = substr($time,0,2);
	$date = substr($time,2,2);
	$year = substr($time,4,4);
	$hour = substr($time,8,2);
	$minute = substr($time,10,2);
	$backupDate = $month."/".$date."/".$year." ".$hour.":".$minute;
	$fileName=$db."_".$time;

	if($i%2==0)
	{
		$classtext="tddark";
	}
	else
	{
		$classtext="tdlight";
	}
	$i++;
	$detailFileName = str_replace(".sql","",$time);
	$detailFileName = str_replace(".sql.gz","",$time);
	echo"<tr><td align='center' class='$classtext'>".$db."_".$time."</td><td align='center' class='$classtext'>".$backupDate."</td><td align='center' class='$classtext'>".$filesize." kb</td>";
	echo "<td align='center' class='$classtext' class='textfont'>".$databaseBackUp->confirm_link("import.php?db=$db&file=$fileName", "Do you really want to Import $file into database: $db?","Import")."</td>";
	echo "<td align='center' class='$classtext' class='textfont'><A HREF='javascript:popUp(\"file_info.php?file=".$db."_".$detailFileName.".info\")'>Details</A></td>";
	echo "<td align='center' class='$classtext' class='textfont'>";
	echo $databaseBackUp->confirm_link("sql.php?do=del_file&file=$fileName", "Do you really want to remove export: $file ?","Delete")."</td></tr>\n";
	?>
<?php
}
if($arrayLength > 0)
{
	echo"</table>\n<br>";
}
if($arrayLength == 0)
{
	echo"<br><table cellspacing='0' border='0' cellpadding='1' width='600' align='left'>";
	echo"<tr><td align='center' colspan='5'><span class='mesg'>$M_NoFileToImport</span></td></tr>";
	echo"<tr><td align='center' colspan='5'>&nbsp;</td></tr>";
	//echo"<tr><td align='center' colspan='5'><input type='image' src='../../images/help.gif'></td></tr>";
	echo"</table>\n<br><br>";
}
?>
<br><br>
</td>
</tr>
 </table>
 </table>
   </td>
   </tr>
   </table>
	 <input type="hidden" name="fileName" value="">
	 <input type="hidden" name="db_name" value="<?php print "$db_name";?>">
	 <input type="hidden" name="save" value="Save">
   </form>
</body>
</html>
